package com.lian.hope.controller;

import com.aliyuncs.kms.transform.v20160120.ListResourceTagsResponseUnmarshaller;
import com.lian.hope.annotations.RequiredPermission;
import com.lian.hope.enums.PermissionType;
import com.lian.hope.model.dto.DynamicCommentDTO;
import com.lian.hope.model.dto.DynamicDTO;
import com.lian.hope.model.po.DynamicComment;
import com.lian.hope.model.vo.DynamicCommentVO;
import com.lian.hope.model.vo.DynamicVO;
import com.lian.hope.model.vo.PageBean;
import com.lian.hope.service.DynamicService;
import com.lian.hope.utils.JwtUtil;
import com.lian.hope.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Options;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/dynamics")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "动态模块相关接口", description = "动态模块相关接口")
public class DynamicController {

    private final DynamicService dynamicService;

    @DeleteMapping("/{dynamicId}")
    @Operation(summary = "根据动态id删除动态")
    public Result deleteDynamicById(@Parameter(description = "动态id") @PathVariable Long dynamicId) {
        dynamicService.deleteDynamicById(dynamicId);
        return Result.success();
    }

    /**
     * 新增动态
     *
     * @param dynamic
     * @return
     */

    @Operation(summary = "新增动态")
    @PostMapping
    @RequiredPermission(PermissionType.DYNAMIC)
    public Result insertDynamic(@RequestBody DynamicDTO dynamic, @RequestHeader String Authorization) {

        Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        Long userId = Long.parseLong(claims.get("userId").toString());

        dynamicService.insertDynamic(dynamic, userId);
        return Result.success();
    }

    /**
     * 分页获取动态
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    @Operation(summary = "分页活动动态")
    @Parameters({@Parameter(description = "页码"), @Parameter(description = "每页数量")})
    public Result<PageBean<DynamicVO>> listDynamics(Integer pageNum, Integer pageSize) {
        PageBean<DynamicVO> dynamicList = dynamicService.listDynamic(pageNum, pageSize);
        return Result.success(dynamicList);
    }

    /**
     * 添加动态点赞
     *
     * @param dynamicId
     * @param userId
     * @return
     */
    @PostMapping("/{dynamicId}/like")
    @Operation(summary = "动态点赞")
    public Result insertDynamicLike(@Parameter(description = "动态id") @PathVariable Long dynamicId,
        @RequestHeader String Authorization) {
        Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        Long userId = Long.parseLong(claims.get("userId").toString());

        Boolean isLike = dynamicService.insertDynamicLike(dynamicId, userId);
        if (isLike) {
            return Result.error("已点赞");
        }
        return Result.success();
    }

    /**
     * 取消用户点赞接口
     *
     * @param dynamicId
     * @return
     */
    @DeleteMapping("/{dynamicId}/cancel-like")
    @Operation(summary = "取消点赞")
    public Result deleteDynamicLike(@Parameter(description = "动态id") @PathVariable Long dynamicId,
        @RequestHeader String Authorization) {
        Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        Long userId = Long.parseLong(claims.get("userId").toString());
        dynamicService.deleteDynamicLikeById(dynamicId, userId);
        return Result.success();
    }

    /**
     * 创建动态评论
     *
     * @return
     */
    @PostMapping("/{dynamicId}/comment")
    @Operation(summary = "创建动态评论")
    public Result insertDynamicComment(@RequestBody DynamicCommentDTO dynamicCommentDTO,
        @RequestHeader String Authorization, @PathVariable Long dynamicId) {

        Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        Long userId = Long.parseLong(claims.get("userId").toString());

        dynamicService.insertDynamicComment(dynamicCommentDTO,dynamicId, userId);
        return Result.success();
    }

    /**
     * 根据动态id获取动态评论列表
     *
     * @param pageNum
     * @param pageSize
     * @param dynamicId
     * @return
     */
    @GetMapping("/{dynamicId}/comments")
    @Operation(summary = "根据动态id获取动态评论列表")
    @Parameters({@Parameter(description = "页码"), @Parameter(description = "每页数量")})
    public Result<PageBean<DynamicCommentVO>> listDynamicComment(Integer pageNum, Integer pageSize,
        @PathVariable Long dynamicId) {
        PageBean<DynamicCommentVO> dynamicComment = dynamicService.listDynamicCommentById(pageNum, pageSize, dynamicId);
        return Result.success(dynamicComment);
    }

    @GetMapping("/{dynamicId}/detail")
    @Operation(summary = "根据动态id获取动态详情")
    public Result<DynamicVO> getDynamicById(@Parameter(description = "动态id") @PathVariable Long dynamicId) {
        DynamicVO dynamicVO = dynamicService.getDynamicById(dynamicId);
        return Result.success(dynamicVO);
    }

    /**
     * 检测用户是否已点赞
     *
     * @param dynamicId
     * @param Authorization
     * @return
     */
    @GetMapping("/{dynamicId}/is-like")
    @Operation(summary = "用户是否已点赞该动态")
    public Result<Boolean> checkUserLikeDynamic(@Parameter(description = "动态id") @PathVariable Long dynamicId,
        @RequestHeader String Authorization) {

        Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        Long userId = Long.parseLong(claims.get("userId").toString());
        Boolean isLike = dynamicService.checkUserLikeDynamic(dynamicId, userId);
        return Result.success(isLike);
    }

}
